package com.example.work3

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

const val MainMsg = "MainMsg"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        //在MainActivity中点击第1个按钮启动第1个二级Activity并正向传参，第1个二级Activity显示接受的参数内容
        val btnToActivity2 = findViewById<Button>(R.id.btnToActivity2)
        btnToActivity2.setOnClickListener {
            val intent = Intent(this, Activity2::class.java) //创建Intent对象，指定要跳转的目标Activity
            val msg = findViewById<EditText>(R.id.input1) //获取输入框中的内容
            intent.putExtra(MainMsg,msg.text.toString()) //将输入框中的内容作为参数传递给目标Activity2
            startActivity(intent) //启动目标Activity
        }

        //在MainActivity中点击第2个按钮启动第2个二级Activity，第2个二级Activity中输入内容确定后可将内容返回MainActivity并显示返回结果
        val btnToActivity3 = findViewById<Button>(R.id.btnToActivity3)
        btnToActivity3.setOnClickListener {
            val intent = Intent(this, Activity3::class.java) //创建Intent对象，指定要跳转的目标Activity
            startActivityForResult(intent,1) //启动目标Activity3
        }

        //在MainActivity中点击第3个按钮启动第3个二级Activity，第3个二级Activity中采用隐式调用的方式启动系统相机拍照（参考附件pdf），拍照成功后在第3个二级Activity中显示照片
        val btnToActivity4 = findViewById<Button>(R.id.btnToActivity4)
        btnToActivity4.setOnClickListener {
            val intent = Intent("android.media.action.IMAGE_CAPTURE") //创建Intent对象，指定要跳转的目标Activity4
            startActivityForResult(intent,2) //启动目标Activity
        }

        //在MainActivity中点击第4个按钮启动第4个二级Activity，第4个二级Activity实现StopWatch功能（参考视频StopWatch），要求横屏竖屏情况下计时器都能连续计时
        val btnToActivity5 = findViewById<Button>(R.id.btnToActivity5)
        btnToActivity5.setOnClickListener {
            val intent = Intent(this, Activity5::class.java) //创建Intent对象，指定要跳转的目标Activity
            startActivity(intent) //启动目标Activity

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1)
        {
            if (resultCode == 1)
            {
                val result = data?.getStringExtra("result")
                val tvResult = findViewById<TextView>(R.id.textView)
                tvResult.text = result
            }
        }
        if (requestCode == 2)
        {
            if (resultCode == RESULT_OK) {
                val imageBitmap = data?.extras?.get("data") as Bitmap
                val imageView = findViewById<ImageView>(R.id.imageView)
                imageView.setImageBitmap(imageBitmap)
            }
        }
    }
}